<?php

namespace common\models\datas;

use Yii;

/**
 * This is the model class for table "{{%customer_subscribe}}".
 *
 * @property integer $id
 * @property string $nickname
 * @property string $openid
 * @property integer $sex
 * @property string $province
 * @property string $city
 * @property string $headimgurl
 * @property integer $subscribe_time
 * @property integer $groupid
 * @property string $remark
 * @property string $unionid
 */
class CustomerSubscribe extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%customer_subscribe}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['sex', 'subscribe_time', 'groupid','is_subscribe','updated_at','site_id'], 'integer'],
            [['nickname', 'openid', 'unionid'], 'string', 'max' => 100],
            [['province', 'city'], 'string', 'max' => 50],
            [['headimgurl', 'remark'], 'string', 'max' => 255]
        ];
    }


    public function fields(){
        $fields = parent::fields();
        $fields['subscribe_time'] = function(){
            return Yii::$app->formatter->asDatetime($this->subscribe_time);
        };
        $fields['updated_at'] = function(){
            if($this->updated_at > 0){
                return Yii::$app->formatter->asDatetime($this->updated_at);
            }else{
                return Yii::t('common', '--');;
            }

        };
        $fields['groupid'] = function(){
            if($this->groupid == 2){
                return Yii::t('common', '非用户组');
            }else{
                return Yii::t('common', '用户组');
            }
        };
        $fields['is_subscribe'] = function(){
            if($this->is_subscribe == 1){
                return Yii::t('common', '已关注');
            }else{
                return Yii::t('common', '取消关注');
            }
        };
        $fields['sex'] = function(){
           if($this->sex == 1){
               return Yii::t('common', '男');
           }elseif($this->sex == 2){
               return Yii::t('common', '女');
           }else{
               return Yii::t('common', '--');
           }
        };
        return $fields;
    }

    public function extraFields(){
        return [
            'operate' => function () {
                if($this->groupid == 2)
                    $checked = '';
                else
                    $checked = 'checked';
                return '<input name="ids[]"  value="'.$this->id.'" data-groupid="'.$this->groupid.'" '.$checked.' type="checkbox"  class="input-checkbox">';


            },
        ];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自增id'),
            'nickname' => Yii::t('common', '微信昵称'),
            'openid' => Yii::t('common', 'openid'),
            'sex' => Yii::t('common', '性别（1男2女）'),
            'province' => Yii::t('common', '省'),
            'city' => Yii::t('common', '市'),
            'headimgurl' => Yii::t('common', '头像'),
            'subscribe_time' => Yii::t('common', '关注时间'),
            'groupid' => Yii::t('common', '分组id'),
            'remark' => Yii::t('common', '备注'),
            'is_subscribe' => Yii::t('common', '是否关注'),
            'updated_at' => Yii::t('common', '修改时间'),
            'unionid' => Yii::t('common', '只有在用户将公众号绑定到微信开放平台帐号后，才会出现该字段。'),
            'site_id' => Yii::t('common', '分店id'),
        ];
    }
}
